<?php

namespace app\api\controller\wechat;

use Think\Db;
use Think\Session;
use think\Controller; // dengh quxiao
use app\api\controller\common\SmsDemo;
use app\api\controller\contractmanage\Commonxc;
class Webwechat extends Controller
{
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
    }

    public function getTabName($exhibition_id)
    {
        $result = db('exhibition')->where(['id' => $exhibition_id])->find();
        $tabName = '';
        if ($result) {
            $tabName = $result['create_tablename'];
        } else {
            return json(['code' => 0, 'msg' => '缺少关键字段', 'data' => '']);
        }
        return $tabName;
    }

    public function getTabName1($exhibition_id)
    {
        $result = db('exhibition')->where(['id' => $exhibition_id])->find();
        $tabName = '';
        if ($result) {
            $tabName = $result['create_person_table'];
        } else {
            return json(['code' => 0, 'msg' => '缺少关键字段', 'data' => '']);
        }
        return $tabName;
    }

    //获取展会数据
    public function getExhibition()
    {
        if (request()->isPost()) {
            $resList = db('exhibition')->where(['is_delete' => 0,'is_wechat'=>1])->order('project_id asc')->select();
            foreach ($resList as &$val) {
                $val['title']=$val['wechat_name'];
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => $resList));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //获取展会数据
    public function getExhibitionDetail()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $res=db('exhibition')->where(['id'=>$exhibition_id,'is_delete'=>0])->find();
            $pavilion=db('pavilion')->field('id,title')->where(['exhibition_id'=>$exhibition_id,'is_delete'=>0])->find();
            return json(array('code' => '1', 'msg' => '成功', 'data' => $res,'pavilion'=>$pavilion));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //获取展厅数据
    public function getHallList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $sql = "select * from crm_hall where pavilion_id in (select id from crm_pavilion where exhibition_id=" . $exhibition_id . " and is_delete=0 ) and is_delete=0";
            $list = Db::query($sql);
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //根据展商列表
    public function getBusinessList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $page = isset($post['page']) && $post['page'] != '' ? $post['page'] : 1;
            $crm_phone = isset($post['crm_phone']) && $post['crm_phone'] != '' ? $post['crm_phone'] : '001';
            $pageSize = isset($post['pageSize']) && $post['pageSize'] != '' ? $post['pageSize'] : 10;
            $hall_id = isset($post['hall_id']) && $post['hall_id'] != '' ? $post['hall_id'] : '';
            $keyword = isset($post['keyword']) && $post['keyword'] != '' ? $post['keyword'] : '';
            $classA = isset($post['classA']) && $post['classA'] != '' ? $post['classA'] : '';
            $classB = isset($post['classB']) && $post['classB'] != '' ? $post['classB'] : '';
            $tabName = $this->getTabName($exhibition_id);
            $tabName1 = $this->getTabName1($exhibition_id);
            $leable=[];
            $per=db($tabName1)->where(['mobile_person'=>$crm_phone])->find();
            if($per){
                if($per['examine_type']==1){
                    if($per['question1']){
                        $res=json_decode($per['question1'],true);
                        if(is_array($res) && count($res)>0 && $res[0]['answer'] && is_array($res[0]['answer'])){
                            $leable=$res[0]['answer'];
                        }
                    }
                }else{
                    if($page>=2){
                        return json(array('code' => '0', 'msg' => ' 审核中，请耐心等待，审核通过后可查看完整信息！'));
                    }
                }
            }else{
                if($page>=2){
                    return json(array('code' => '2', 'msg' => ' 查看更多展商/展品信息请进行观众登记！'));
                }
            }

            $param = '';
            if ($classB) {
                $param .= " AND b.second_category_id like '%$classB%' ";
            }else{
                if ($classA) {
                    $param .= " AND b.category_id like '%$classA%' ";
                }else{
                    if(count($leable)>0 && $page==1){
                        $param .= " AND b.category_id like '%$leable[0]%' ";
                    }
                }
            }
            if ($keyword) {
                $bus=db($tabName)->where(['company_name'=>['like',"%$keyword%"],'status'=>['egt',3],'account'=>'is not null'])->select();
                if(!empty($bus)){
                    $param .= " AND b.company_name like '%$keyword%' ";
                }else{
                    $goodRes=db('zsgoodszp')->where(['title'=>['like',"%$keyword%"]])->select();
                    if(!empty($goodRes)){
                        if($hall_id){
                            $hallRes = db('hall')->where(['id' => $hall_id, 'is_delete' => 0])->find();
                            if (empty($hallRes)) {
                                return json(array('code' => '0', 'msg' => '展厅信息错误'));
                            }
                            $hallList=db('booth')->where(['is_delete'=>0,'hall_id'=>$hall_id,'agent_id'=>['egt',0],'exhibition_id'=>$exhibition_id])->select();
                            $ids=[];
                            foreach ($goodRes as &$val) {
                                $ids[]=$val['id_zs'];
                            }
                            if(!empty($hallList)){
                                foreach ($hallList as &$vals) {
                                    $ids[]=$vals['agent_id'];
                                }
                            }
                            $ids=array_unique($ids);
                            $param .=" ADN b.id in (".$ids.implode(',',$ids).") ";
                        }else {
                            $param .= " AND b.id in (SELECT distinct id_zs FROM crm_zsgoodszp WHERE title like '%$keyword%' AND exhibition_id=" . $exhibition_id . ") ";
                        }
                    }else{
                        $param .= " AND b.company_name like '%$keyword%' ";
                    }
                }
            }
            if (!$keyword && $hall_id) {
                $hallRes = db('hall')->where(['id' => $hall_id, 'is_delete' => 0])->find();
                if (empty($hallRes)) {
                    return json(array('code' => '0', 'msg' => '展厅信息错误'));
                }
                $param .= " AND b.id in (select agent_id FROM crm_booth WHERE is_delete=0 AND hall_id=" . $hall_id . " AND agent_id>0)";
            }

            $sql = "SELECT distinct b.id,b.area_city,b.company_name,b.logo,b.company_address,b.category_id,b.status,p.id as postion_id,p.number,p.hall_id,p.area FROM crm_" . $tabName . " AS b";
            $sql .= " LEFT JOIN crm_booth AS p ON p.agent_id=b.id AND p.exhibition_id=$exhibition_id";
            //$sql .= " LEFT JOIN crm_zsgoodszp AS z ON z.id_zs=b.id AND z.exhibition_id=".$exhibition_id." AND is_del=0 ";
            $sql .= " WHERE b.label=1" . $param;
            $sql .=" order by p.area desc ";
            $total=Db::query($sql);
            $sql .= " limit " . ($page - 1) * $pageSize . ",$pageSize";
            $list = Db::query($sql);
            foreach ($list as &$val) {
                $val['logo'] = '/public/uploads/image/'.($val['logo']?$val['logo'] : $exRes['pic']);
                $hall=db('hall')->where(['id'=>$val['hall_id']])->find();
                $goods=db('zsgoodszp')->where(['id_zs'=>$val['id'],'is_del'=>0,'exhibition_id'=>$exhibition_id])->find();
                $val['name']=$goods?$goods['title']:'';
                $val['number']=$hall['title']."-".$val['number'];
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => ['list' => $list, 'count' => count($total)]));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //验证方法
    public function wechatLogin(){
        if (request()->isPost()) {
            $post = input('post.');
            $crm_phone = isset($post['crm_phone']) && $post['crm_phone'] != '' ? $post['crm_phone'] : '';
            $phone = isset($post['phone']) && $post['phone'] != '' ? $post['phone'] : '';
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$crm_phone || !$phone){
                return json(array('code' => '0', 'msg' => '电话号码有误'));
            }
            $tabName = $this->getTabName1($exhibition_id);
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $res=db($tabName)->where(['mobile_person'=>$crm_phone])->find();
            if($res){
                if($res['examine_type'] == 1){
                    return json(array('code' => '1', 'msg' => '验证成功','data'=>$crm_phone));
                }else{
                    return json(array('code' => '0', 'msg' => '您注册信息还未通过审核,请联系管理员'));
                }
            }else{
                return json(array('code' => '0', 'msg' => '您还不是展会观众，快去登记注册一个吧'));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    public function uniquArr($array){
        $result = array();
        foreach($array as $k=>$val){
            $code = false;
            foreach($result as $_val){
                if($_val['id'] == $val['id']){
                    $code = true;
                    break;
                }
            }
            if(!$code){
                $result[]=$val;
            }
        }
        return $result;
    }

    //根据展位获取展商详细信息
    public function getBusinessDetail()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $tabName = $this->getTabName($exhibition_id);
            $data = db($tabName)->where(['id' => $id])->find();
            if (empty($data)) {
                return json(array('code' => '0', 'msg' => '展商信息错误'));
            }
            $goodsList = db('zsgoodszp')->where(['is_del' => 0, 'exhibition_id' => $exhibition_id, 'id_zs' => $id])->select();
            if (!empty($goodsList)) {
                foreach ($goodsList as &$val) {
                    $val['pic'] = $val['pic'] ? unserialize($val['pic']) : [];
                    $val['category'] = $val['category'] ? unserialize($val['category']) : [];
                }
            }
            $link = array();
            if ($data['second_category_id']) {
                $categoryArr = explode(",", $data['second_category_id']);
                if (!empty($categoryArr)) {
                    $link = db($tabName)->where(['second_category_id' => ['like', '%' . $categoryArr[0] . '%'],'status'=>['egt',5],'id'=>['neq',$id]])->limit(0, 10)->select();
                    foreach($link as &$v){
                        $v['logo'] = '/public/uploads/image/'.($v['logo']?$v['logo'] : $exRes['pic']);
                    }
                }
            }

            $bsql="select b.*,pt.title as hall_title from crm_booth as b left join crm_hall as pt on pt.id = b.hall_id where b.id in (select id_zwei from crm_contract where id_zhui=".$exhibition_id." and id_zs=".$id." and is_delete=0) and b.is_delete=0 order by b.id desc";
            $both_list=Db::query($bsql);
            $hall='';
            foreach($both_list as &$_val){
                $hall.=$_val['hall_title']."-".$_val['number'].' ';
            }
            $data['logo'] = '/public/uploads/image/'.($data['logo']?$data['logo'] : $exRes['pic']);
            return json(array('code' => '1', 'msg' => '成功', 'data' => $data, 'goodsList' => $goodsList, 'link' => $link, 'postionList' => $both_list,'hall'=>$hall));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //获得展品
    public function getGoodsDetail()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'exhibition_id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $tabName = $this->getTabName($exhibition_id); //
            $resData = db($tabName)->where(['id' => $id])->find();
            if (empty($resData)) {
                return json(array('code' => '0', 'msg' => '展商信息错误'));
            }
            $list = db('zsgoodszp')->where(['is_del' => 0, 'exhibition_id' => $exhibition_id, 'id_zs' => $id])->select();
            if (!empty($list)) {
                foreach ($list as &$val) {
                    $resList['pic'] = $val['pic'] ? unserialize($val['pic']) : [];
                    $resList['category'] = $val['category'] ? unserialize($val['category']) : [];
                }
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //获得展品
    public function getGoodsList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $keyWord = isset($post['keyWord']) && $post['keyWord'] != '' ? $post['keyWord'] : '';
            $page = isset($post['page']) && $post['page'] != '' ? $post['page'] : 1;
            $pageSize = isset($post['pageSize']) && $post['pageSize'] != '' ? $post['pageSize'] : 10;
            $classA = isset($post['classA']) && $post['classA'] != '' ? $post['classA'] : '';
            $classB = isset($post['classB']) && $post['classB'] != '' ? $post['classB'] : '';
            $crm_phone = isset($post['crm_phone']) && $post['crm_phone'] != '' ? $post['crm_phone'] : '001';
            $tabName = $this->getTabName($exhibition_id); //
            $tabName1 = $this->getTabName1($exhibition_id);
//            $leable=[];
//            $per=db($tabName1)->where(['mobile_person'=>$crm_phone])->find();
//            if($per){
//                if($per['examine_type']==1){
//                    if($per['question1']){
//                        $res=json_decode($per['question1'],true);
//                        if(is_array($res) && count($res)>0 && $res[0]['answer'] && is_array($res[0]['answer'])){
//                            $leable=$res[0]['answer'];
//                        }
//                    }
//                }else{
//                    if($page>=2){
//                        return json(array('code' => '0', 'msg' => ' 审核中，请耐心等待，审核通过后可查看完整信息！'));
//                    }
//                }
//            }else{
//                if($page>=2){
//                    return json(array('code' => '2', 'msg' => '查看更多展商/展品信息请进行观众登记！'));
//                }
//            }
            $param = '';
//            if ($classB) {
//                $param .= " AND z.category like '%$classB%' ";
//                $param .= " AND z.id_zs in (select id from crm_".$tabName." where company_name like '%$keyWord%')";
//            }else{
//                if ($classA) {
//                    $param .= " AND z.category like '%$classA%' ";
//                    $param .= " AND z.id_zs in (select id from crm_".$tabName." where company_name like '%$keyWord%')";
//                }
////                else{
////                    if(count($leable)>0 && $page==1) {
////                        $param .= " AND z.category like '%$leable[0]%' ";
////                    }
////                }
//            }
            $param.= " AND z.is_del=0 AND z.exhibition_id=".$exhibition_id;
            if ($keyWord) {
                $bus=db($tabName)->where(['company_name'=>['like',"%$keyWord%"],'account'=>'is not null'])->select();
                if(!empty($bus)){
                    $param .= " AND z.id_zs in (select id from crm_".$tabName." where company_name like '%$keyWord%')";
                }else{
                    $param .= " AND z.title like '%$keyWord%' ";
                }
            }else{
                if ($classB) {
                    $param .= " AND z.id_zs in (select id from crm_".$tabName." where category_id like '%$keyWord%')";
                }else{
                    if ($classA) {
                        $param .= " AND z.id_zs in (select id from crm_".$tabName." where second_category_id like '%$keyWord%')";
                    }
                }
            }

//            $sql=" SELECT z.*,p.company_name,p.logo,p.id AS company_id FROM crm_zsgoodszp AS z ";
//            $sql.=" LEFT JOIN crm_".$tabName." AS p ON p.id=z.id_zs ";
            $sql=" SELECT z.*,p.company_name,p.logo,p.id AS company_id FROM crm_zsgoodszp AS z ";
            $sql.=" LEFT JOIN crm_".$tabName." AS p ON p.id=z.id_zs ";
            $sql .= " WHERE 1=1 " . $param;
            $sql .= " order by z.id desc";
            $sql .= " limit " . ($page - 1) * $pageSize . ",$pageSize";
//          var_dump($sql);die();
            $list = Db::query($sql);
            if (!empty($list)) {
                foreach ($list as &$val) {
                    $val['logo'] = '/public/uploads/image/'.($val['logo']?$val['logo'] : $exRes['pic']);
                    $val['pic'] = $val['pic'] ? unserialize($val['pic']) : [];
                    $val['category'] = $val['category'] ? unserialize($val['category']) : [];
                }
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //获得展品
    public function getGoodsDetailInfo()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $res=db('zsgoodszp')->where(['id'=>$id,'exhibition_id'=>$exhibition_id,'is_del'=>0])->find();

            if (!empty($res)) {
                $res['pic'] = $res['pic'] ? unserialize($res['pic']) : [];
                $res['category'] = $res['category'] ? unserialize($res['category']) : [];
                $tabName = $this->getTabName($exhibition_id); //
                $resData = db($tabName)->where(['id' => $res['id_zs']])->find();
                $resPos = db('booth')->where(['agent_id' => $res['id_zs'],'is_delete'=>0,'exhibition_id'=>$exhibition_id])->find();
                if($resPos){
                    $hallRes = db('hall')->where(['id' => $resPos['hall_id'], 'is_delete' => 0])->find();
                    $resPos['number']=$hallRes?$hallRes['title']."-".$resPos['number']:$resPos['number'];
                }
                if(!empty($resData)){
                    $resYs=db('zsgoodszp')->where(['id_zs'=>$resData['id'],'exhibition_id'=>$exhibition_id,'is_del'=>0])->find();
                    $resData['logo'] = '/public/uploads/image/'.($resData['logo']?$resData['logo'] : $exRes['pic']);
                    $resData['goodsname']=$resYs?$resYs['title']:'';
                }
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => ['goods_detail'=>$res,'company'=>$resData,'position'=>$resPos]));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //推荐展商
    public function getRecommendList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'exhibition_id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $tabName = $this->getTabName($exhibition_id); //
            $resData = db($tabName)->where(['id' => $id])->find();
            if (empty($resData)) {
                return json(array('code' => '0', 'msg' => '展商信息错误'));
            }
            $list = array();
            if ($resData['second_category_id']) {
                $categoryArr = explode(",", $resData['second_category_id']);
                if (!empty($categoryArr)) {
                    $list = db($tabName)->where(['second_category_id' => ['like', '%' . $categoryArr[0] . '%'],'account'=>['exp','is not null']])->find();
                }
            }
            foreach ($list as &$val) {
                $val['logo'] = '/public/uploads/image/'.($val['logo']?$val['logo'] : $exRes['pic']);
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //邀请排行版
    public function getRankingList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $tabName = $this->getTabName($exhibition_id);
            $tabName1 = $this->getTabName1($exhibition_id);
            $sql = "SELECT
            a.id,a.company_name,a.name_cn,a.name_en,a.logo,COUNT(b.company_parent_id) AS num
            FROM crm_" . $tabName . " AS a
            LEFT JOIN crm_" . $tabName1 . " AS b ON a.id = b.company_parent_id
            WHERE b.company_parent_id>0
            GROUP BY
                a.id
            ORDER BY
            num DESC";
            $list = Db::query($sql);
            $self=-1;$data='';
            foreach ($list as $key=>&$val) {
                $val['logo'] = '/public/uploads/image/'.($val['logo']?$val['logo'] : $exRes['pic']);
                if($val['id']==$id){
                    $self=$key;
                    $data=$val;
                }
            }
            $distance=0;
            //第一名
            if($self <= 0){
                $distance=0;
            }else{
                $distance=$list[$self-1]['num']-$list[$self]['num'];
            }

            if($self>9){
                $distance=$list[9]['num']-$list[$self]['num'];;
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => ['list'=>$list,'selfInfo'=>['index'=>($self>=0?($self+1):count($list)+1),'distance'=>$distance,'data'=>$data]]));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //邀请排行版
    public function companyRankingList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $tabName1 = $this->getTabName1($exhibition_id);
            $list=db($tabName1)->field("id,name_cn,name_en,company_name,mobile_person,from_unixtime(create_time,'%Y-%m-%d %H:%i:%s') as time")->where(['company_parent_id'=>$id])->order('create_time desc')->select();
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }




    //收藏列表
    public function getCollectionList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $phone = isset($post['phone']) && $post['phone'] != '' ? $post['phone'] : 0;
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $tabName = $this->getTabName($exhibition_id);
            $sql=" select s.*,p.logo,p.company_name,p.area_city,p.company_address,p.category_id,p.logo,h.number,h.hall_id from crm_collection as s ";
            $sql.=" left join crm_".$tabName." as p on p.id=s.company_id ";
//            $sql.=" left join crm_zsgoodszp as g on g.id_zs=s.company_id and g.exhibition_id=".$exhibition_id." AND g.is_del = 0 ";
            $sql.=" left join crm_booth as h on h.agent_id=s.company_id AND h.exhibition_id=$exhibition_id";
            $sql.=" where s.phone=".$phone." and s.is_delete=0 and s.exhibition_id=".$exhibition_id." order by s.update_time desc";
            $list = Db::query($sql);
            foreach ($list as $key=>&$val) {
                $hall=db('hall')->where(['is_delete'=>0,'id'=>$val['hall_id']])->find();
                $goods=db('zsgoodszp')->where(['id_zs'=>$val['id'],'is_del'=>0,'exhibition_id'=>$exhibition_id])->find();
                $val['title']=$goods?$goods['title']:'';
                $val['number']=$hall?$hall['title']."-".$val['number']:$val['number'];
                $val['logo'] = '/public/uploads/image/'.($val['logo']?$val['logo'] : $exRes['pic']);
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //查找展商
    public function  searchCompany(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $keyWord = isset($post['keyWord']) && $post['keyWord'] != '' ? $post['keyWord'] : '';
            $tabName = $this->getTabName($exhibition_id); //
            $param = " and p.status>3 ";
            if ($keyWord) {
                $param .= " AND p.company_name like '%$keyWord%' AND p.account is not null ";
            }
            $sql=" SELECT z.number,z.hall_id,p.id,p.company_name,p.logo,p.id AS company_id FROM crm_".$tabName." AS p ";
            $sql.=" LEFT JOIN crm_booth AS z ON z.agent_id=p.id AND z.exhibition_id=$exhibition_id";
            $sql .= " WHERE 1=1 " . $param;
            $list = Db::query($sql);
            foreach ($list as $key=>&$val) {
                $hall=db('hall')->where(['is_delete'=>0,'id'=>$val['hall_id']])->find();
                $val['number']=$hall?$hall['title']."-".$val['number']:$val['number'];
                $val['logo'] = '/public/uploads/image/'.($val['logo']?$val['logo'] : $exRes['pic']);
            }
            return json(array('code' => '1', 'msg' => '成功', 'data' => $list));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //添加收藏
    public function collectionAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $phone = isset($post['phone']) && $post['phone'] != '' ? $post['phone'] : '';
            $company_id = isset($post['company_id']) && $post['company_id'] != '' ? $post['company_id'] : '';
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            if (!$phone || !$company_id) {
                return json(array('code' => '0', 'msg' => '关键参数不能为空'));
            }
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $comRes = db('collection')->where(['phone' => $phone, 'company_id' => $company_id,'exhibition_id'=>$exhibition_id])->find();
            if ($comRes) {
                $is_delete=$comRes['is_delete']==0?1:0;
                $res = db('collection')->where(['id' => $comRes['id']])->update(['is_delete'=>$is_delete,'update_time'=>time()]);
                if(!$res){
                    return json(array('code' => '0', 'msg' => '操作失败'));
                }
                return json(array('code' => '1', 'msg' => '操作成功'));
            }else{
                $data['phone'] = $phone;
                $data['company_id'] = $company_id;
                $data['exhibition_id'] = $exhibition_id;
                $data['create_time'] = time();
                $data['update_time'] = time();
                $res = db('collection')->insert($data);
                if (!$res) {
                    return json(array('code' => '0', 'msg' => '操作失败'));
                }
                return json(array('code' => '1', 'msg' => '操作成功'));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //删除收藏
    public function collectionDel()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $resCol = db('collection')->where(['is_delete' => 0, 'id' => $id])->find();
            if (empty($resCol)) {
                return json(array('code' => '0', 'msg' => '收藏记录错误'));
            }
            $data['is_delete'] = 1;
            $res = db('collection')->where(['id' => $id])->update($data);
            if (!$res) {
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //判断此展商是否收藏
    public function collectionIsShow()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $company_id = isset($post['company_id']) && $post['company_id'] != '' ? $post['company_id'] : '';
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $comRes = db('collection')->where(['company_id' => $company_id, 'exhibition_id' => $exhibition_id])->find();
            var_dump($comRes);die();
            $res = $comRes ? $comRes['is_delete'] : 1;
            return json(array('code' => '1', 'msg' => '操作成功', 'data' => $res));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //合成图片
    public function getPosterImg(){
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            //展会
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $tabName = $this->getTabName($exhibition_id);
            //展商
            $resData = db($tabName)->where(['id' => $id])->find();
            if (empty($resData)) {
                return json(array('code' => '0', 'msg' => '展商信息错误'));
            }
            $resData['logo'] = '/public/uploads/image/'.($resData['logo']?$resData['logo'] : $exRes['pic']);
            //展品
            $goodsList = db('zsgoodszp')->where(['is_del' => 0, 'exhibition_id' => $exhibition_id, 'id_zs' => $id])->select();
            if (!empty($goodsList)) {
                foreach ($goodsList as &$val) {
                    $val['pic'] = $val['pic'] ? unserialize($val['pic']) : [];
                    $val['category'] = $val['category'] ? unserialize($val['category']) : [];
                }
            }
            //展位
            $posRes = db('booth')->where(['agent_id' => $id, 'is_delete' => 0,'exhibition_id'=>$exhibition_id])->find();
            if($posRes){
                $hall=db('hall')->where(['id' => $posRes['hall_id'], 'is_delete' => 0])->find();
                $posRes['number']=$hall?$hall['title']."-".$posRes['number']:$posRes['number'];
            }
            //数据整合
            $imgArr=[];
            if(count($goodsList)>0){
                foreach ($goodsList as $v){
                    if(count($v['pic'])>0){
                        foreach ($v['pic'] as $vv){
                            if(count($imgArr)<3){
                                $imgArr[]=$vv;
                            }
                        }
                    }

                }
            }
            return json(array('code' => '1', 'data'=>array('imgArr'=>$imgArr,'postion'=>$posRes,'company'=>$resData,'exhibition'=>$exRes)));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    public function smsPhone(){
        if (request()->isPost()) {
            $post = input('post.');
            $phone = isset($post['phone']) && $post['phone'] != '' ? $post['phone'] : '';
            $sms_autograph = isset($post['sms_autograph']) && $post['sms_autograph'] != '' ? $post['sms_autograph'] : '';
            $sms_code = isset($post['sms_code']) && $post['sms_code'] != '' ? $post['sms_code'] : '';
            $smsData = isset($post['smsData']) && $post['smsData'] != '' ? $post['smsData'] : array();
            if (!$phone) {
                return json(array('code' => '0', 'msg' => '电话错误'));
            }
            if (!$sms_autograph) {
                return json(array('code' => '0', 'msg' => '签名错误'));
            }
            if (!$sms_code) {
                return json(array('code' => '0', 'msg' => '模板CODE错误'));
            }
            if(!empty($smsData)){
                $smsData=json_decode($smsData,true);
            }
            //var_dump($smsData);die();
            $SmsDemo=new SmsDemo();
            $smsEmil = $SmsDemo->sendSms($phone,$sms_autograph,$sms_code,$smsData);
            if(is_object($smsEmil) && $smsEmil->Code== 'OK'){
                return json(['code'=>1,'msg'=>'短信发送成功']);
            }else{
                return json(['code'=>0,'msg'=>'短信发送失败','data'=>$smsEmil]);
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //判断展商负责人
    public function companyPerson()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $phone = isset($post['phone']) && $post['phone'] != '' ? $post['phone'] : '';
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            $exRes = db('exhibition')->where(['is_delete' => 0, 'id' => $exhibition_id])->find();
            if (empty($exRes)) {
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $tabName = $this->getTabName($exhibition_id);
            $comRes = db($tabName)->where(['mobile' => $phone])->find();
            return json(array('code' => '1', 'msg' => '操作成功', 'data' => !empty($comRes)?$comRes['id']:''));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }
}
